package com.padakeji.android.ui.examples;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.padakeji.android.ui.widget.AutoLoadRecycler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by atanl on 2015/10/23.
 */
public class AutoLoadRecyclerTestActivity extends Activity {

    private AutoLoadRecycler autoLoadRecycler;
    private AutoLoadRecycler.Adapter mAdapter;
    private List<String> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autoloadrecycler_test);

        findViews();

        data = new ArrayList<>();
        int count = 15;
        for (int i = 0; i < count; ++i) {
            data.add("item" + i);
        }
        mAdapter = new MyAdapter(this);

        configViews();
    }

    private void findViews() {
        autoLoadRecycler = (AutoLoadRecycler) findViewById(R.id.activity_autoLoadRecyclerTest_recycler_items);
    }

    private void configViews() {
        autoLoadRecycler.setAdapter(mAdapter);
        autoLoadRecycler.setLayoutManager(new LinearLayoutManager(this));
    }


    private class MyAdapter extends AutoLoadRecycler.Adapter {

        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        public RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
            View v = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false);
            MyViewHolder vh = new MyViewHolder(v);
            vh.tv = (TextView) v.findViewById(android.R.id.text1);
            return vh;
        }

        @Override
        public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((MyViewHolder)holder).tv.setText(data.get(position));
        }

        @Override
        public int itemCount() {
            return data.size();
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }
}
